
define(["./dom", "./console", "./messages", "./builder", "./ajax", "_"], function(dom, console, messages, builder, ajax, _) {
  var alert, dismissAll, dismissOne, exports, findInnerContainer, getURL, removeAlert, setupUI, severityToClass;
  severityToClass = {
    success: "alert alert-success",
    warn: "alert alert-warning",
    error: "alert alert-error"
  };
  getURL = function(container) {
    return container.attribute("data-dismiss-url");
  };
  removeAlert = function(container, alert) {
    alert.remove();
    if (container.find(".alert").length === 0) {
      return container.update(null);
    }
  };
  dismissAll = function(container) {
    var alerts;
    console.debug("dismiss all");
    alerts = container.find("[data-alert-id]");
    if (alerts.length === 0) {
      container.update(null);
      return;
    }
    return ajax(getURL(container), {
      success: function() {
        return container.update(null);
      }
    });
  };
  dismissOne = function(container, button) {
    var alert, id;
    console.debug("dismiss single");
    alert = button.parent();
    id = alert.attribute("data-alert-id");
    if (!id) {
      removeAlert(container, alert);
      return;
    }
    return ajax(getURL(container), {
      parameters: {
        id: id
      },
      success: function() {
        return removeAlert(container, alert);
      }
    });
  };
  setupUI = function(container) {
    var clickHandler;
    clickHandler = function() {
      dismissAll(container);
      return false;
    };
    container.update(builder(".well", [
      "div", {
        "data-container-type": "inner"
      }
    ], [
      ".row-fluid > button.btn.btn-mini.pull-right", {
        onclick: clickHandler
      }, ["strong", "\u00d7 "], messages("core-dismiss-label")
    ]));
    return container.on("click button.close", function() {
      dismissOne(container, this);
      return false;
    });
  };
  findInnerContainer = function() {
    var outer;
    outer = dom.body().findFirst("[data-container-type=alerts]");
    if (!outer) {
      console.error("Unable to locate alert container element to present an alert.");
      return null;
    }
    if (!outer.element.firstChild) {
      setupUI(outer);
    }
    return outer != null ? outer.findFirst("[data-container-type=inner]") : void 0;
  };
  alert = function(data) {
    var className, container, content, element;
    container = findInnerContainer();
    if (!container) {
      return;
    }
    className = severityToClass[data.severity] || "alert";
    content = data.markup ? data.message : dom.escapeHTML(data.message);
    element = builder("div", {
      "class": className
    }, ["button.close", "\u00d7"], content);
    if (data.id) {
      element.attribute("data-alert-id", data.id);
    }
    container.append(element);
    if (data.transient) {
      return _.delay(removeAlert, exports.TRAINSIENT_DURATION, container, element);
    }
  };
  alert.TRAINSIENT_DURATION = 5000;
  return exports = alert;
});
